<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:reservation:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:reservation:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:reservation:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:reservation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reservationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="楼盘名称" align="center" prop="name" />
      <el-table-column label="预约人" align="center" prop="showingsName" />
      <el-table-column label="联系电话" align="center" prop="showingsMobile" />
      <el-table-column label="预约房源" align="center" prop="room_name" />
      <el-table-column label="预约时间" align="center" prop="reserveTime" />
      <el-table-column label="预约状态" align="center" prop="status" >
        <template scope="scope">
          <span>{{scope.row.status==1?"已看房":"待看房"}}</span>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改预约房源对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="addform" label-width="80px">
        <el-form-item label="楼盘名称">
          <el-input v-model="louname" placeholder="请输入楼盘名称" @change="lou" />
        </el-form-item>

        <el-form-item label="预约人">
          <el-input v-model="addform.showingsName" placeholder="请输入预约人姓名" />
        </el-form-item>

        <el-form-item label="联系电话">
          <el-input v-model="addform.showingsMobile" placeholder="请输入联系电话" />
        </el-form-item>

        <el-form-item label="房源名称">
<!--          <el-input v-model="addform.roomName" placeholder="请输入房源名称" />-->
          <el-radio-group v-model="addform.roomId">
            <el-radio v-for="(item, index) in housings" :key="index" :label="item.id"
                         :disabled="item.disabled">{{item.roomName}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="预约时间">
<!--          <el-input v-model="form.reserveTime" placeholder="请输入预约时间" />-->
          <div class="block">
            <el-date-picker
              v-model="addform.reserveTime"
              type="datetime"
              placeholder="选择预约日期时间">
            </el-date-picker>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReservation, getReservation, delReservation ,addReservation, updateReservation } from "@/api/housings/yuyue/reservation";
import {listUser, selLou} from "@/api/housings/add/addfen";
import {selHousing} from "@/api/housings/xinxi/info"
import {addyuyue} from "@/api/housings/yuyue/reservation"

export default {
  name: "Reservation",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约房源表格数据
      reservationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      housings:[],
      //添加的楼盘名称
      louname:'',
      // 表单参数
      addform: {
        roomId:'',
        showingsName:'',
        showingsMobile:'',
        reserveTime:''
      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getuser()
  },
  methods: {

    /*添加查询楼盘是否存在*/
    lou(){
      console.log(this.louname)
      if(this.louname==null || this.louname==""){
        return false;
      }
      selLou(this.louname).then(res=>{
        console.log(res.ss)
        if(res.ss==200){
          alert("楼盘查找成功")
          selHousing(res.msg).then(res=>{
            this.housings=res
            console.log(this.housings)
          })
        }else{
          alert("没有此楼盘")
        }
      })
    },

    /** 查询预约房源列表 */
    getList() {
      this.loading = true;
      listReservation(this.queryParams).then(response => {
        this.reservationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        roomId: null,
        customerId: null,
        showingsName: null,
        showingsSex: null,
        showingsMobile: null,
        userId: null,
        reserveTime: null,
        status: 0,
        createTime: null,
        remark: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预约房源";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReservation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预约房源";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.addform)
      addyuyue(this.addform).then(res=>{
        if(res){
          alert("成功")
        }else{
          alert("失败")
        }
      })


      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.id != null) {
      //       updateReservation(this.form).then(response => {
      //         this.msgSuccess("修改成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     } else {
      //       addReservation(this.form).then(response => {
      //         this.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除预约房源编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delReservation(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/reservation/export', {
        ...this.queryParams
      }, `system_reservation.xlsx`)
    }
  }
};
</script>
